import SnowMan from "../Matter/SnowMan";
import GlobalEvent from "../../global/GlobalEvent";
import SnowBall from "../Matter/SnowBall";
import GamePlayView from "./GamePlayView";
import MatterManager from "../../managers/MatterManager";
import GamePlay from "../../model/GamePlay";

export default class GameControl extends Laya.Script {


    /** @prop {name:snowManPrefab,tips:"雪人",type:Prefab}*/
    snowManPrefab: Laya.Prefab;

    /** @prop {name:snowBallPrefab,tips:"雪球",type:Prefab}*/
    snowBallPrefab: Laya.Prefab;


    snowMan: Laya.Sprite;
    manMatter: SnowMan;
    realOwner: GamePlayView;
    isMove = false;

    gameItems = [];

    constructor() { super(); }


    onEnable(): void {
        this.realOwner = this.owner as GamePlayView;


        this.addSnowManItem();
        this.addRobots(1);

        // 以下事件绑定可以转移到SnowMan内部
        GlobalEvent.instance.on('wheel_start', this, () => {
            this.manMatter.startMove();
        })

        GlobalEvent.instance.on('wheel_change', this, (angle: number) => {
            this.manMatter.changeDirection(angle);
        })

        GlobalEvent.instance.on('wheel_stop', this, () => {
            this.manMatter.stopMove();
        })

    }

    addRobot() {
        let robot = this.snowManPrefab.create() as Laya.Sprite;
        let mapCenterPoint = GamePlay.instance.mapCenterPoint;
        let mapRadius = GamePlay.instance.mapRadius * 0.7;
        let x = mapCenterPoint.x + Math.random() * mapRadius * 2 - mapRadius;
        let y = mapCenterPoint.y + Math.random() * mapRadius * 2 - mapRadius;

        robot.pos(x, y);
        this.realOwner.addChild(robot);
        let snowManComp = robot.getComponent(SnowMan) as SnowMan;
        snowManComp.isRobot = true;
        this.gameItems.push(snowManComp);
    }


    addRobots(count: number) {
        for(let i = 0; i < count; i++) {
            this.addRobot();
        }
    }

    onUpdate() {
        this.realOwner.x = -this.snowMan.x + Laya.stage.width / 2;
        this.realOwner.y = -this.snowMan.y + Laya.stage.height / 2;
    }

    addSnowManItem() {
        this.snowMan = this.snowManPrefab.create();
        let mapCenterPoint = GamePlay.instance.mapCenterPoint;
        this.snowMan.pos(mapCenterPoint.x, mapCenterPoint.y);

        this.realOwner.addChild(this.snowMan);

        this.manMatter = this.snowMan.getComponent(SnowMan) as SnowMan;

        this.gameItems.push(this.manMatter);
    }

    onDisable(): void {
        GlobalEvent.instance.offAllCaller(this);
        for (const gamtItem of this.gameItems) {
            // gamtItem.removeSelf();
            gamtItem.owner.removeSelf();
        }
        this.gameItems = [];
    }


    checkCrash() {
        for (const gamtItem of this.gameItems) {
            if (gamtItem.snowBall && gamtItem.snowBall.body) {
                for (const other of this.gameItems) {
                    if (other !== gamtItem) {
                        
                    }
                }
            }
        }
    }
}